package com.example.powernodemall.service.Impl;

import com.example.powernodemall.entity.SysMenu;
import com.example.powernodemall.mapper.SysMenuMapper;
import com.example.powernodemall.model.SysMenuPage;
import com.example.powernodemall.service.QuerySysMenuService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class QuerySysMenuServiceImpl implements QuerySysMenuService {
    @Resource
    SysMenuMapper sysMenuMapper;
    @Override
    public List<SysMenuPage>  queryPageAuthrization(Long userId) {
        List<SysMenuPage> sysMenuPages=sysMenuMapper.queryPage(userId);
        return transformTree(sysMenuPages,0L);
    }

    private List<SysMenuPage> transformTree(List<SysMenuPage> sysMenuPages, Long i) {
        List<SysMenuPage> collect = sysMenuPages.stream().filter(sysMenuPage -> sysMenuPage.getParentId() == i).collect(Collectors.toList());
        collect.stream().forEach(sysMenuPage -> {
            sysMenuPage.setList(transformTree(sysMenuPages,sysMenuPage.getMenuId()));
        });
        return collect;
    }
}
